package com.jiazhong.project.service;

import com.jiazhong.project.pojo.Brand; // 统一使用 project.pojo 包
import com.jiazhong.project.mapper.BrandMapper;
import com.jiazhong.project.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    // 调用mapper中的selectAll的方法
    public List<Brand> selectAll() {
        final SqlSession session = sqlSessionFactory.openSession();
        BrandMapper brandMapper = session.getMapper(BrandMapper.class);
        final List<Brand> brands = brandMapper.selectAll(); // 统一使用 Brand 类型
        return brands;
    }

    public int insertAll(Brand brand) { // 修改参数类型
        final SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        final int brands = brandMapper.insertAll(brand);
        sqlSession.commit();
        return brands;
    }

    public Brand selectByIdBrand(int id) { // 确保返回类型正确
        final SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Brand brands = brandMapper.selectByIdBrand(id);
        return brands;
    }
    public void update(Brand brand){
        final SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
         brandMapper.updateById(brand);
         sqlSession.commit();
    }
    public void delete(int id){
        final SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.delById(id);
        sqlSession.commit();
    }
}
